<template>
  <base-layout>
    <template #searchPanel>
      <search-panel
        v-slot="{ form }"
        :selectPer="'sh:order:jp:page'"
        @search="search"
        @handleDataExport="handleDataExport"
        :showSearchButton="true"
      >
        <el-date-picker
          v-model="form.paymentTime"
          type="datetimerange"
          start-placeholder="销售开始时间"
          end-placeholder="销售结束时间"
          clearable
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
        />
       <el-select
            clearable
            v-model="form.carBrandId"
            style="width: 150px"
            placeholder="品牌"
            filterable
            @change="handleCarBrand"
          >
            <el-option value="" label="全部" />
            <el-option
              v-for="item in allBrand"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
          <el-select
            clearable
            v-model="form.carSeriesId"
            style="width: 150px"
            placeholder="车系"
            filterable
          >
            <el-option value="" label="全部" />
            <el-option
              v-for="item in allSeries"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
        <el-select
          clearable
          style="width: 150px"
          v-model="form.orderType"
          placeholder="订单类型"
        >
          <el-option
            v-for="(item) in getDict['精品订单类型']"
            :key="item.index"
            :value="item.value"
            :label="item.text"
          />
        </el-select>
        <el-select
          clearable
          style="width: 150px"
          v-model="form.payStatus"
          placeholder="支付状态"
        >
          <el-option
            v-for="item in getDict['支付状态']"
            :key="item.index"
            :value="item.value"
            :label="item.text"
          />
        </el-select>
        <el-input
          v-model="form.client"
          clearable
          placeholder="客户名称/手机号"
          style="width: 150px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="160"
      >
        <template #action="{ row }">
          <i class="el-icon-view"
             title="查看"
             style="cursor: pointer;"
             v-permission="'sh:order:jp:detail'"
             @click="handleDataChange(row, '查看精品订单')"></i>
          <i
            class="el-icon-delete"
            title="删除"
            style="color: red; margin-left: 20px; cursor: pointer"
            v-permission="'sh:order:jp:delete'"
            @click="handleDataDelete(row)"
          ></i>
        </template>
      </data-table>
    </template>
  </base-layout>
</template>
<script>
import baseMixin from '@/mixins/base'
import {
  page,
  update,
  deleteById
} from '@/api/pre-sale/orderManagement/boutiqueOrder'

import { getDict } from '@/utils/index'
import { detailById } from '@/api/pre-sale/advertManagement/story'
import { getAllBrand, getSeries } from '@/api/after-sale/systemManage/carModel'

export default {
  mixins: [
    baseMixin({
      idName: 'id',
      findData: page,
      dataName: ['content', 'totalRecords'],
      deleteData: deleteById,
      updateData: update,
      findDataById: detailById
      // format(res) {
      //   return res.map((data) => {
      //     data.statusName = data.status === true ? '启用' : '禁用'
      //     return data
      //   })
      // }
    })
  ],

  data() {
    return {
      getDict: getDict(['精品订单类型', '支付状态']),
      form: {
        client: '' // 客户信息
      },
      tableColumnAttributes: [
        {
          name: '订单号',
          value: 'orderNo',
          type: 'text'
        },
        {
          name: '所属门店',
          value: 'storeName',
          type: 'text'
        },
        {
          name: '订单类型',
          value: 'orderType',
          type: 'dict',
          dict: '精品订单类型'
        },
        {
          name: '销售时间',
          value: 'saleTime',
          type: 'text'
        },
        {
          name: '客户名称',
          value: 'name',
          type: 'text'
        },
        {
          name: '客户手机',
          value: 'phoneNum',
          type: 'text'
        },
        {
          name: '品牌',
          value: 'carBrandName',
          type: 'text'
        },
        {
          name: '车型',
          value: 'carModelName',
          type: 'text'
        },
        {
          name: '销售顾问',
          value: 'saleName',
          type: 'text'
        },
        {
          name: '精品费用',
          value: 'orderAmount',
          type: 'text'
        },
        {
          name: '数量',
          value: 'detailCount',
          type: 'text'
        },
        {
          name: '安装费用',
          value: 'installCost',
          type: 'text'
        },

        {
          name: '支付状态',
          value: 'payStatus',
          type: 'dict',
          dict: '支付状态'
        },
        {
          name: '库存状态',
          value: 'outStatus',
          type: 'text'
        },
        {
          name: '创建时间',
          value: 'createTime',
          type: 'text'
        }
      ],
      dict: {},
      allBrand: [],
      allSeries: []
    }
  },
  created() {
    this.requestCarBrandList()
  },
  methods: {
    // 请求品牌
    async requestCarBrandList() {
      const { success, data } = await getAllBrand()
      if (success) {
        this.allBrand = data || []
      }
    },
    // 请求车系
    async requestVehicleList(brandId) {
      const { success, data } = await getSeries(brandId)
      if (success) {
        this.form.carSeriesId = ''
        this.allSeries = data || []
      }
    },
    // 切换
    async handleCarBrand(val) {
      if (!val) return
      this.requestVehicleList(val)
    },

    // 查询
    search(form) {
      this.searchOptions = form
      this.currentPage = 1
      this.setTableData()
    },
    handleDataChange(row) {
      this.$router.push({
        name: 'addBoutiqueOrder',
        params: {
          id: row.id
        }
      })
    },
    handleDataExport() {

    }
  },
  mounted() {
  }
}
</script>
